package com.base.caselib;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.caselib.adapter.LawStatuteItemsListAdapter;
import com.base.caselib.adapter.LawStatuteListAdapter;
import com.base.caselib.bean.LawStatuteBean;
import com.base.caselib.bean.LawStatuteDetailListBean;
import com.base.caselib.databinding.ActivityLawStatuteBinding;
import com.base.caselib.presenter.LawStatutePresenter;
import com.base.caselib.view.LawStatuteListView;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.bean.CheckRecordEntity;
import com.base.commonlib.recy.BasicRecyViewHolder;
import com.base.commonlib.recy.utils.LawStatuteSpaceItemDecoration;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zw on 2019/11/13.
 * 法律法规选择
 */

@Route(path = "/case/law_statutelist")
public class LawStatuteActivity extends CBaseBarMvpActivity<LawStatutePresenter> implements LawStatuteListView{
    private ActivityLawStatuteBinding mBinding;
    private LawStatuteSpaceItemDecoration mCustomDivider;
    private LawStatuteSpaceItemDecoration mCustomDividerItems;
    private LawStatuteListAdapter lawStatuteListAdapter;
    private LawStatuteItemsListAdapter lawStatuteListAdapterItems;
    private LawStatuteBean mLawStatuteBean=new LawStatuteBean();
    private List<LawStatuteDetailListBean>  lawStatuteDetailListBeans=new ArrayList<>();

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_law_statute;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityLawStatuteBinding) mRootBinding;
        mPresenter=new LawStatutePresenter(this,this);
        setBackNavigation();
        setBarTitle("项目选择");
        setRigImg(R.drawable.ic_title_save, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<CheckRecordEntity> list = new ArrayList<>();
                lawStatuteDetailListBeans=   lawStatuteListAdapterItems.getSelectDataList();

                for (LawStatuteDetailListBean be:lawStatuteDetailListBeans) {
                    CheckRecordEntity checkRecordEntity=new CheckRecordEntity();
                    checkRecordEntity.setLegislationId(be.getId());
                    checkRecordEntity.setLegislationName(be.getItemAct());
                    checkRecordEntity.setIsMajor(0);
                    checkRecordEntity.setLawId(mLawStatuteBean.getDictId());
                    checkRecordEntity.setLawName(mLawStatuteBean.getDictValue());
                    checkRecordEntity.setLogin_user_id(BaseApp.getUser().getUserId());
                    list.add(checkRecordEntity);
                }

                Intent in = new Intent();
                in.putExtra("checklist",new Gson().toJson(list));
                setResult(RESULT_OK,in);
                finish();
            }
        });

    }

    @Override
    protected void start() {
        Map<String,String> stringMap=new HashMap<>();
        stringMap.put("parentId","0001");
        mPresenter.getLaw(stringMap);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        LinearLayoutManager managerItems = new LinearLayoutManager(mContext);
        if (lawStatuteListAdapter == null) {
            lawStatuteListAdapter = new LawStatuteListAdapter(R.layout.item_law_statute, mContext);
            mCustomDivider = new LawStatuteSpaceItemDecoration();
        }
        if (lawStatuteListAdapterItems == null) {
            lawStatuteListAdapterItems = new LawStatuteItemsListAdapter(R.layout.item_law_statute_items, mContext);
            mCustomDividerItems = new LawStatuteSpaceItemDecoration();
        }
        mBinding.lawRecyclerview1.setLayoutManager(manager);
        mBinding.lawRecyclerview1.setAdapter(lawStatuteListAdapter);
        mBinding.lawRecyclerview1.addItemDecoration(mCustomDivider);

        mBinding.lawRecyclerview2.setLayoutManager(managerItems);
        mBinding.lawRecyclerview2.setAdapter(lawStatuteListAdapterItems);
        mBinding.lawRecyclerview2.addItemDecoration(mCustomDividerItems);

        //第一个列表点击事件
        lawStatuteListAdapter.setItemClickListener(new BasicRecyViewHolder.OnItemClickListener() {
            @Override
            public void OnItemClick(View v, int adapterPosition) {
                if (lawStatuteDetailListBeans.size()>0){
                    lawStatuteDetailListBeans.clear();
                }
                lawStatuteListAdapter.selectTaskListItem(adapterPosition);
                mLawStatuteBean=lawStatuteListAdapter.getData(adapterPosition);
                Map<String,Object> stringMap2=new HashMap<>();
                stringMap2.put("dictId",lawStatuteListAdapter.getData(adapterPosition).getDictId());
                mPresenter.getLawItems(stringMap2);//设置点击的项改变字体颜色
            }
        });

        //第二个列表点击事件
        lawStatuteListAdapterItems.setItemClickListener(new BasicRecyViewHolder.OnItemClickListener() {
            @Override
            public void OnItemClick(View v, int adapterPosition) {
                LawStatuteDetailListBean bean=lawStatuteListAdapterItems.getData(adapterPosition);
                // 获取id bean.getxxxxx

            }
        });

    }

    @Override
    public void returnLawStatuteBean(List<LawStatuteBean> lawStatuteBeanList) {

        if (lawStatuteBeanList.size()>0){
            lawStatuteListAdapter.refreshDatas(lawStatuteBeanList);
            lawStatuteListAdapter.selectTaskListItem(0);
            Map<String,Object> stringMap2=new HashMap<>();
            stringMap2.put("dictId",lawStatuteBeanList.get(0).getDictId());
            mLawStatuteBean=lawStatuteBeanList.get(0);
            mPresenter.getLawItems(stringMap2);
        }

    }

    @Override
    public void returnLawStatuteItemsBean(List<LawStatuteDetailListBean> lawStatuteDetailListBeanList) {
        lawStatuteListAdapterItems.refreshDatas(lawStatuteDetailListBeanList);
       // lawStatuteListAdapterItems.selectTaskListItem(0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }
}
